// pages/ucenter/index/index.js

var app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        daili:true,
        loginflag:false, 
        url:"https://vkceyugu.cdn.bspapp.com/VKCEYUGU-b1ebbd3c-ca49-405b-957b-effe60782276/c8c4e05e-42d4-4a22-b111-85765d4e22e2.jpg",
        upName:"张三三",
        upId:"000001"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var page = this
        var userinfo = wx.getStorageSync('userInfo')

        if (userinfo){
            app.globalData.userInfo = userinfo
            page.setData({
                loginflag:true
            })
        }

        this.setData({
            userInfo:app.globalData.userInfo
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title:'sharetest',
            path:'/pages/index/index?shareid=' + '54185418'
        }
    },
    bindgetuserinfo:function(e){
        var page = this
        var userinfo = wx.getStorageSync('userInfo');
        console.log(userinfo)
        if (userinfo) {
            return;
        }
        
        console.log(e.detail.userInfo)
        if (e.detail.userInfo){
            page.data.loginflag = true;
            page.setData({
                loginflag:true
            })
            wx.setStorage({
              data: e.detail.userInfo,
              key: 'userInfo',
            })
            this.setData({
                userInfo:e.detail.userInfo
            })
            app.globalData.userInfo = e.detail.userInfo;
        }
    },
    loginout:function(){

        var page = this
        var userInfo = {
            nickName: 'Hi,游客',
            userName: '点击去登录',
            avatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
        }
        
        wx.showModal({
          confirmText: '确定',
          showCancel: true,
          title: '您确定退出登录吗？',
          success: (result) => {
            if (result.confirm){
                page.data.loginflag = false
                this.setData({
                    loginflag:false
                })
                app.globalData.userInfo = userInfo
                wx.removeStorageSync('userInfo')
                this.setData({
                    userInfo:app.globalData.userInfo
                })
            }
            
          },
          fail: (res) => {},
          complete: (res) => {},
        })
        
    },
    address:function(){
        let that = this;
        wx.chooseAddress({
            success (res) {
              let address = res.provinceName + res.cityName + res.countyName + res.detailInfo;
              console.log(address);
            },
            fail:function(res){
                console.log(res);
            }
        })
    }
})